﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;
using System.Web;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace NLBS.Control
{
    public class FormRewriterControlAdapter : System.Web.UI.Adapters.ControlAdapter
    {
        protected override void Render(System.Web.UI.HtmlTextWriter writer)
        {
            base.Render(new RewriteFormHtmlTextWriter(writer));
        }
    }
    public class HtmlInputControlAdapter : System.Web.UI.Adapters.ControlAdapter
    {
        protected override void Render(HtmlTextWriter writer)
        {
           
            base.Render(new RewriteFormHtmlTextWriter2(writer,this.Control));
        }
    }

    public class HtmlControlAdapter : System.Web.UI.Adapters.ControlAdapter
    {
        protected override void Render(HtmlTextWriter writer)
        {
            //base.Render(writer);
            base.Render(new RewriteFormHtmlTextWriter2(writer, this.Control));
        }
    }

    public class SelectControlAdapter : System.Web.UI.Adapters.ControlAdapter
    {
        public System.Web.UI.HtmlControls.HtmlSelect c;
        protected override void Render(HtmlTextWriter writer)
        {
            if (this.Control is HtmlSelect)
            {
                c = this.Control as HtmlSelect;
                foreach (ListItem item in c.Items)
                {
                    if (item.Text.StartsWith("$R{") && item.Text.EndsWith("}"))
                    {
                        string ResourcesText = NLBS.Resources.ResourceManager.GetString(item.Text.Replace("$R{", "").Replace("}","").ToLower());
                        if (!string.IsNullOrEmpty(ResourcesText))
                        {
                            item.Text = ResourcesText;
                        }
                    }
                }
            }
            base.Render(new RewriteFormHtmlTextWriter2(writer, c));
        }
    }

}

public class RewriteFormHtmlTextWriter2 : HtmlTextWriter
{
   public Control c;
    
    public RewriteFormHtmlTextWriter2(HtmlTextWriter writer,Control c)
        : base(writer)
    {
        this.InnerWriter = writer.InnerWriter;
        this.c = c;
    }

    public override void WriteAttribute(string name, string value, bool fEncode)
    {
        if ((name == "name") || name == "id")
        {
            value = c.ID;
        }
        if (c is HtmlInputRadioButton)
        {
            HtmlInputRadioButton r = c as HtmlInputRadioButton;

            if ((name == "name"))
            {
                value = r.Name;

            }
        }
        base.WriteAttribute(name, value, fEncode);
    }
}




public class RewriteFormHtmlTextWriter : HtmlTextWriter
{

    public RewriteFormHtmlTextWriter(HtmlTextWriter writer)
        : base(writer)
    {
        this.InnerWriter = writer.InnerWriter;
    }

    public RewriteFormHtmlTextWriter(System.IO.TextWriter writer)
        : base(writer)
    {
        base.InnerWriter = writer;
    }


    public override void WriteAttribute(string name, string value, bool fEncode)
    {

        // If the attribute we are writing is the "action" attribute, and we are not on a sub-control, 
        // then replace the value to write with the raw URL of the request - which ensures that we'll 
        // preserve the PathInfo value on postback scenarios 

        if ((name == "action"))
        {

            HttpContext Context;
            Context = HttpContext.Current;

            if (Context.Items["ActionAlreadyWritten"] == null)
            {

                // Because we are using the UrlRewriting.net HttpModule, we will use the 
                // Request.RawUrl property within ASP.NET to retrieve the origional URL 
                // before it was re-written. You'll want to change the line of code below 
                // if you use a different URL rewriting implementation. 

                value = Context.Request.RawUrl;

                // Indicate that we've already rewritten the <form>'s action attribute to prevent 
                // us from rewriting a sub-control under the <form> control 

                Context.Items["ActionAlreadyWritten"] = true;

            }

        }

        base.WriteAttribute(name, value, fEncode);

    }

}
